<template >
    <div>
        <m-table :data="data" :loading="loading" :title="$route.meta.title" :columns="columns">
            <template #extra>
                <a-button type="primary" shape="round" v-perms="['/adminapi/system.organize.position/save']" @click="handleEdit('add')">
                    <template #icon>
                        <icon-plus />
                    </template>
                </a-button>
            </template>


            <template #action="{ record }">
                <a-space>
                    <a-button type="primary" shape="round" v-perms="['/adminapi/system.organize.position/update']" @click="handleEdit('edit', record)">
                        <template #icon>
                            <icon-edit />
                        </template>
                    </a-button>

                    <a-popconfirm :content="`确定删除 [${record.name}] 吗？`" @ok="handleDelete(record.id)">
                        <a-button type="primary" shape="round" v-perms="['/adminapi/system.organize.position/delete']" status="danger">
                            <template #icon>
                                <icon-delete />
                            </template>
                        </a-button>
                    </a-popconfirm>
                </a-space>
            </template>
        </m-table>
        <edit ref="editRef" @success="run" />
    </div>
</template>

<script setup lang="ts">
import { getPositionList , deletePosition} from '@/api/system/organize/position'
import { useRequest } from 'vue-request'
import Edit from './components/edit.vue'
const editRef = ref<InstanceType<typeof Edit>>()
const { data, loading  , run} = useRequest(getPositionList)

const columns = [
    {
        title: '职位名称',
        dataIndex: 'name'
    },{
        title:'所属部门',
        dataIndex:'department.name'
    },
    {
        title:'操作',
        dataIndex:'action',
        slotName:'action'
    }
    
]


const handleEdit = (type: string , record?: any) => {
    editRef.value?.open(type)
    if (record) {
        nextTick(() => {
            editRef.value?.setFormData(record)
        })
    }
}

const handleDelete = async (id: number) => {
    await deletePosition(id)
    run()
}

</script>

